package cn.ecpay.unionpay.settle.dao;

import cn.ecpay.unionpay.settle.entity.db.TPvFundSettleSingle;
import cn.ecpay.unionpay.settle.entity.db.TPvQuotaReduction;
import cn.ecpay.unionpay.settle.entity.db.TPvSingleQuery;
import org.rex.DB;
import org.rex.db.Ps;
import org.rex.db.exception.DBException;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 额度调减
 * Created by zhaoyanzeng on 2018/12/12.
 */
@Repository
public class QuotaReductionDao {
    /**
     * 插入
     * @param
     * @throws DBException
     */
    public void insert(TPvQuotaReduction quotaReduction) throws DBException {
        String sql = "INSERT INTO T_PV_QUOTA_REDUCTION" +
                " (ID,txn_No,txn_Type,ACQ_INS_CODE,TXN_DATE,SND_TIME,CURRENCY_CODE,TXN_AMT,remark,resp_Code,resp_Msg,avlb_Quota_Amt,RESP_VALIDATE)" +
                " VALUES" +
                " (#{id},#{txnNo},#{txnType},#{acqInsCode},#{txnDate},#{sndTime},#{currencyCode},#{txnAmt},#{remark},#{respCode},#{respMsg},#{avlbQuotaAmt}," +
                "#{respValidate})";
        DB.update(sql,quotaReduction);
    }

    /**
     *根据ID 获取交易信息
     * @param id
     * @return
     * @throws DBException
     */
    public TPvQuotaReduction findById(String id) throws DBException {
        String sql = "SELECT * FROM T_PV_QUOTA_REDUCTION WHERE ID=?";
        return DB.get(sql,new Object[]{id},TPvQuotaReduction.class);
    }

    /**
     *根据流水号获取交易信息
     * @param txnNo 流水号
     * @return
     * @throws DBException
     */
    public TPvQuotaReduction findByTxnNo(String txnNo) throws DBException {
        String sql = "SELECT * FROM T_PV_QUOTA_REDUCTION WHERE TXN_NO=?";
        return DB.get(sql,new Object[]{txnNo},TPvQuotaReduction.class);
    }

    /**
     * 当天交易流水号不能重复
     * @param txnNo
     * @param txnDate
     * @return
     * @throws DBException
     */
    public boolean isExistTxn(String txnNo, String txnDate) throws DBException {
        Ps ps = new Ps();
        String sql = "SELECT * FROM T_PV_QUOTA_REDUCTION WHERE TXN_NO=? and TXN_DATE =?";
        ps.add(txnNo);
        ps.add(txnDate);
        List<TPvFundSettleSingle> list = DB.getList(sql,ps,TPvFundSettleSingle.class);
        if(list!= null && list.size()>0){
            return true;
        }
        return false;
    }
}
